package com.image.androidimagebroswer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.image.androidimagebroswer.adapter.GridListAdapter;
import com.image.androidimagebroswer.data.MediaData;
import com.image.androidimagebroswer.R;

import java.util.List;

/**
 * Created by liuzhao.futrue on 2018/8/26.
 * <p>
 * 从官方文档中学习，分析
 * <p>
 * 点击事件的实现：
 * 1、recyclerView.addOnItemTouchListener();
 * 2、创建RecyclerView的时候设置点击事件；（选择）setOnItemClickListener 写到Adapter中，并且保证灵活性
 * 3、ItemView attach ReyclerView的时候
 */

public class GridListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        final List<String> photoPaths = MediaData.getPhotoList();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);
        RecyclerView recyclerView = findViewById(R.id.rv_gridlist);
        recyclerView.setLayoutManager(new GridLayoutManager(GridListActivity.this, 3));

        dealAdapter(photoPaths, recyclerView);

    }

    private void dealAdapter(final List<String> photoPaths, RecyclerView recyclerView) {
        GridListAdapter adapter = new GridListAdapter(GridListActivity.this, photoPaths);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new GridListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.i("lz", "onItemClick " + photoPaths.get(position));
                // 列表页进入大图界面
                Intent intent = new Intent(GridListActivity.this,BigImageActivity.class);
                intent.putExtra("index",position);
                startActivity(intent);
            }
        });
    }
}
